package com.iretailer.report.influxdb.service;

import com.iretailer.report.dto.EasAlarmModel;
import com.iretailer.report.influxdb.dao.InfluxdbDao;
import com.iretailer.report.influxdb.model.EasAlarm;
import com.iretailer.report.influxdb.util.TableName;
import com.iretailer.report.model.DeviceMall;
import com.iretailer.report.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: xjw
 * @Date: 2019/9/25 0025 14:29
 * @Description:
 */
@Service
public class InfluxdbEasAlarmService {

    @Autowired
    private InfluxdbDao influxdbDao;

    public int insertEasAlarm(List<EasAlarm> post, TableName.EasAlarm table) {
        int result = -1;
        int separateNumber = 50000;
        int size = post.size();
        if (size <= separateNumber) {
            result = influxdbDao.batchInsertEasAlarm(post, table);
        } else {
            int batchSize = size / separateNumber;
            int remain = size % separateNumber;
            for (int i = 0; i < batchSize; ) {
                int index = i * separateNumber;
                int toIndex = index + separateNumber;
                result += influxdbDao.batchInsertEasAlarm(post.subList(index, toIndex), table);
            }
            if (remain > 0) {
                result += influxdbDao.batchInsertEasAlarm(post.subList(size - remain, size), table);
            }

        }
        return result;
    }

    public List<EasAlarm> getZero(String timeZone, TableName.EasAlarm table, List<Long> zoneIds, String startTime, String endTime) {
        //客流置零
        List<EasAlarm> oldFlow = selectEasAlarmGrid(timeZone, table, zoneIds, startTime, endTime);
        oldFlow.forEach(it -> {
            it.setAlarmTimes(0);
        });
        return oldFlow;
    }

    public List<EasAlarm> selectEasAlarmGrid(String timeZone, TableName.EasAlarm table, List<Long> zoneIds, String startTime, String endTime) {
        return influxdbDao.selectEasAlarmGrid(timeZone, table, zoneIds, startTime, endTime);
    }

    public List<EasAlarm> selectEasAlarm(String timezone, TableName.EasAlarm table, List<Long> zoneIds, String startTime, String endTime, String period) {
        return influxdbDao.selectEasAlarmByZoneId(timezone, table, zoneIds, startTime, endTime, period);
    }

    public List<EasAlarm> selectEasAlarmWeek(String timezone, TableName.EasAlarm table, List<Long> zoneIds, String startTime, String endTime, String period, String offset) {
        return influxdbDao.selectEasAlarmGroup_offset_ByZoneId(timezone, table, zoneIds, startTime, endTime, period, offset);
    }

    public List<EasAlarmModel> getDeviceId_PeriodEasAlarm(List<DeviceMall> deviceMalls, String startTime, String endTime, String period) {
        List<EasAlarmModel> result = new ArrayList<>();
        if (deviceMalls != null && deviceMalls.size() > 0) {
            Map<String, List<DeviceMall>> timezoneMap = deviceMalls.stream().collect(Collectors.groupingBy(DeviceMall::getTimeZone));
            timezoneMap.entrySet().forEach(it -> {
                String timeZone = it.getKey();
                Map<Long, DeviceMall> dmMap = it.getValue().stream().collect(Collectors.toMap(DeviceMall::getId, a -> a));
                List<EasAlarm> easAlarms = null;
                if (period.equals("7d")) {
                    easAlarms = selectEasAlarmWeek(timeZone, TableName.EasAlarm.EasAlarm_B, new ArrayList<>(dmMap.keySet()), startTime, endTime, period, "-3d");
                } else {
                    easAlarms = selectEasAlarm(timeZone, TableName.EasAlarm.EasAlarm_B, new ArrayList<>(dmMap.keySet()), startTime, endTime, period);
                }
                if (easAlarms != null && easAlarms.size() > 0) {
                    for (EasAlarm e : easAlarms) {
                        DeviceMall deviceMall = dmMap.get(Long.valueOf(e.getZoneid()));
                        EasAlarmModel model = new EasAlarmModel();
                        model.setMallId(deviceMall.getMallId());
                        model.setMallName(deviceMall.getMallName());
                        model.setZonedDateTime(e.getTime());
                        String time = TimeUtil.zoneDateTime_format(e.getTime());
                        model.setDateTime(time);
                        model.setDeviceId(deviceMall.getId());
                        model.setAlarmTimes(e.getAlarmTimes());

                        result.add(model);
                    }
                }
            });
        }
        return result;
    }
}
